{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 稀疏张量的定义",
   "id": "b8d12011bb3a04a5"
  },
  {
   "metadata": {
    "trusted": false,
    "ExecuteTime": {
     "end_time": "2025-11-11T05:25:11.623762Z",
     "start_time": "2025-11-11T05:25:04.797802Z"
    }
   },
   "cell_type": "code",
   "source": "import torch\n\nindices = torch.tensor([[0,1,1],[2,0,2]])\nvalues  = torch.tensor([1,2,3])\ncoo     = torch.sparse_coo_tensor(indices, values, [4,4], dtype=torch.float32, device='cuda:0')\n\nprint(coo.to_dense())\n",
   "id": "initial_id",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0., 0., 1., 0.],\n",
      "        [2., 0., 3., 0.],\n",
      "        [0., 0., 0., 0.],\n",
      "        [0., 0., 0., 0.]], device='cuda:0')\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 推荐系统中的用户-物品矩阵",
   "id": "be50d8633eb44988"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-04T08:30:39.203460Z",
     "start_time": "2025-11-04T08:30:39.194326Z"
    },
    "trusted": false
   },
   "cell_type": "code",
   "source": "import torch\n\n# 模拟一个用户-物品评分矩阵，5个用户，4件物品\n# 非零元素代表用户对物品有评分\nindices = torch.tensor([[0, 0, 1, 2, 3, 4],   # 用户索引\n                        [1, 3, 0, 2, 1, 3]],  # 物品索引\n                       dtype=torch.long\n                       )\nvalues = torch.tensor([5., 3., 4., 2., 5., 1.]) # 评分值\nuser_item_space  = torch.sparse_coo_tensor(indices, values, [5, 4])\n\nprint(\"用户-物品稀疏评分矩阵:\")\nprint(user_item_space.to_dense())\n\n# 计算用户形似度(计算第一个用户与其他用户的相似度)\ntarget_user = 0\ntarget_vector = user_item_space.to_dense()[target_user]\n\n# print(target_vector.unsqueeze(0))\n\n# # 由于矩阵系数，我们可以避免大量的0运算\nsimilarities = []\nfor i in range(5):\n    if (i == target_user):\n        continue\n    user_vec = user_item_space.to_dense()[i]\n    # 简单的宇轩相似度计算（实际应用可能使用更高效的方式）\n    sim = torch.cosine_similarity(target_vector.unsqueeze(0), user_vec.unsqueeze(0))\n    print(f\"smi: {sim}\")\n    similarities.append( (i, sim.item()) )\n\nprint(f\"用户{target_user} 与其他用户的相似度\")\nfor user, sim in similarities:\n    print(f\"用户{user}: {sim:.4f}\")\n",
   "id": "2bd30b27c0df5d03",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "用户-物品稀疏评分矩阵:\ntensor([[0., 5., 0., 3.],\n        [4., 0., 0., 0.],\n        [0., 0., 2., 0.],\n        [0., 5., 0., 0.],\n        [0., 0., 0., 1.]])\nsmi: tensor([0.])\nsmi: tensor([0.])\nsmi: tensor([0.8575])\nsmi: tensor([0.5145])\n用户0 与其他用户的相似度\n用户1: 0.0000\n用户2: 0.0000\n用户3: 0.8575\n用户4: 0.5145\n"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "trusted": false
   },
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "3a74a694a4e79610"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
